from fractions import Fraction
def matrix(array_list):
    print(len(array_list) ** 0.5)
    if len(array_list) == 9:
        print('==============')
        ls = array_list
        ls1 = [Fraction(1, 1),0,0,
            0,Fraction(1, 1),0,
            0,0,Fraction(1, 1)]
        #初始化
        s = 0
        for i in ls: 
            ls[s] = Fraction(i)
            s += 1
        print('题目为：')
        print_ar(ls,ls1)
        mp1 = ls[3]/ls[0]
        mp2 = ls[6]/ls[0]
        ls[3] = 0
        ls[6] = 0
        ls[4] = ls[4]-ls[1]*mp1
        ls[5] = ls[5]-ls[2]*mp1
        ls1 = e(ls1,2,1,mp1)
        ls[7] = ls[7]-ls[1]*mp2
        ls[8] = ls[8]-ls[2]*mp2
        ls1 = e(ls1,3,1,mp2)
        ms = '第一步：r2 - {}r1,r3 - {}r1'.format(mp1,mp2)
        print(ms)
        print_ar(ls,ls1)
        mp3 = ls[7]/ls[4]
        ls[7] = 0
        ls[8] = ls[8]-ls[5]*mp3
        ls1 = e(ls1,3,2,mp3)
        ms = '第二步：r3 - {}r2'.format(mp3)
        print(ms)
        print_ar(ls,ls1)
        print('第三步：')
        gy('r1', ls[0])
        gy('r2', ls[4])
        gy('r3', ls[8])
        m1 = ls[0]
        m2 = ls[4]
        m3 = ls[8]
        s = 0
        for i in ls:
            if s < 3 :
                ls[s] = i*Fraction(1, m1)               
            else:
                ls[s] = i*Fraction(1, m2)
            s += 1
        s = 0
        for i in ls1:
            if s < 3 :
                ls1[s] = i*Fraction(1, m1)              
            elif s < 6:
                ls1[s] = i*Fraction(1, m2)
            else:
                ls1[s] = i*Fraction(1, m3)
            s += 1
        ls[8] = 1
        print_ar(ls,ls1)
        mp2 = ls[2]/ls[8]
        mp1 = ls[5]/ls[8]
        ls1 = e(ls1,2,3,mp1)
        ls1 = e(ls1,1,3,mp2)
        ls[2] = 0
        ls[5] = 0
        ms = '第四步：r2 - {}r3,r1 - {}r3'.format(mp1,mp2)
        print(ms)
        print_ar(ls,ls1)
        mp3 = ls[1]/ls[4]
        ls[1] = 0
        ls1 = e(ls1,1,2,mp3)
        ms = '最后一步：r1 - {}r2'.format(mp3)
        print(ms)
        print_ar(ls,ls1)
    return ''
#归一
def gy(r,b):
    if b != 1 and b != 0:
        print('{}*({})'.format(r,Fraction(1,b)))
    return ''
def print_ar(list1,list2):
    s = 0 
    print('===============')
    print('原矩阵：')
    for i in list1:
        if s == 2 or s == 5 or s == 8:
            print(list1[s])
        else:
            print(list1[s],end=' ')
        s += 1
    print('可逆矩阵：')
    s = 0
    for i in list2:
        if s == 2 or s == 5 or s == 8:
            print(list2[s])
        else:
            print(list2[s],end=' ')
        s += 1
    print('===============')
    return ''
def e(list,ra,rb,mp):
    r1 = list[0:3]
    r2 = list[3:6]
    r3 = list[6:9]
    if ra == 1:
        ra1 = r1
    elif ra == 2:
        ra1 = r2
    elif ra == 3:
        ra1 = r3
    if rb == 1:
        rb1 = r1
    elif rb == 2:
        rb1 = r2
    elif rb == 3:
        rb1 = r3
    ra1[0] = ra1[0]-rb1[0]*mp
    ra1[1] = ra1[1]-rb1[1]*mp
    ra1[2] = ra1[2]-rb1[2]*mp
    if ra == 1:
        r1 == ra1
    elif ra == 2:
        r2 = ra1
    elif ra == 3:
        r3 = ra1
    if rb == 1:
        r1 = rb1
    elif rb == 2:
        r2 = rb1
    elif rb == 3:
        r3 = rb1
    list = r1+r2+r3
    return list

#括号里为3阶方阵
print(matrix([1,1,2,2,2,3,4,3,3]))


